home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Especial Multimedia
/
Especial Multimedia.iso
/
Multimed
/
Prg
/
IMAGELIB.ZIP
/
MIMAGE.ZIP
/
REG_IM20.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-07-28
|
41KB
|
1,344 lines
{add me to the delphi component library
Copyright 1995 by
Kevin Adams, 74742,1444
Jan Dekkers, 72130,353
}
{Part of Imagelib VCL/DLL Library.
Written by Jan Dekkers and Kevin Adams}
unit Reg_im20;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Forms, Controls,
extctrls, StdCtrls, DLL20LIN, menus, DB, DBTables, Mask, Buttons;
type
TMultiImage = class(TGraphicControl)
private
FPicture : TPicture;
FAutoSize : Boolean;
FStretch : Boolean;
FCenter : Boolean;
FReserved : Byte;
FFilename : TFileName;
Fdither : byte;
FResolution : byte;
FSaveQuality : byte;
FSaveSmooth : byte;
FSaveFileName : TFileName;
Temps : TFileName;
function GetCanvas: TCanvas;
procedure PictureChanged(Sender: TObject);
procedure SetAutoSize(Value: Boolean);
procedure SetCenter(Value: Boolean);
procedure SetPicture(Value: TPicture);
procedure SetStretch(Value: Boolean);
protected
function GetPalette: HPALETTE; override;
public
BFiletype : String;
Bwidth : Integer;
BHeight : Integer;
Bbitspixel : Integer;
Bplanes : Integer;
Bnumcolors : Integer;
BSize : Longint;
Bcompression : String;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Canvas: TCanvas read GetCanvas;
function GetMultiBitmap : String;
Procedure WriteMultiName(Name : String);
procedure Paint; override;
function GetSmooth : Byte;
procedure SetSmooth(smooth : Byte);
function GetQuality : Byte;
procedure SetQuality(Quality : Byte);
function GetDither : Byte;
procedure SetDither(dith : Byte);
function GetRes : Byte;
procedure SetRes(res : Byte);
function GetSaveFileName : TFilename;
procedure SetSaveFileName(fn : TFilename);
procedure SaveAsJpg(FN : TFileName);
procedure SaveAsBMP(FN : TFileName);
function GetInfoAndType(filename : TFilename) : Boolean;
published
property Align;
property AutoSize: Boolean read FAutoSize write SetAutoSize default False;
property Center: Boolean read FCenter write SetCenter default False;
property DragCursor;
property DragMode;
property Enabled;
property JPegDither : Byte read GetDither write SetDither;
property JPegResolution : Byte read GetRes write SetRes;
property Picture: TPicture read FPicture write SetPicture;
property JPegSaveQuality : Byte read GetQuality write SetQuality;
property JPegSaveSmooth : Byte read GetSmooth write SetSmooth;
property DefSaveFileName : TFileName read GetSaveFileName write SetSaveFileName;
property ImageName : String read GetMultiBitmap write WriteMultiName;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Stretch: Boolean read FStretch write SetStretch default False;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
{ TDBMultiImage }
Type
TDBMultiImage = class(TCustomControl)
private
FDataLink : TFieldDataLink;
FPicture : TPicture;
FBorderStyle : TBorderStyle;
FAutoDisplay : Boolean;
FStretch : Boolean;
FCenter : Boolean;
FPictureLoaded : Boolean;
FUpdateAsJpeg : Boolean;
FReserved : Byte;
Fdither : byte;
FResolution : byte;
FSaveQuality : byte;
FSaveSmooth : byte;
procedure DataChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
function GetReadOnly: Boolean;
procedure PictureChanged(Sender: TObject);
procedure SetAutoDisplay(Value: Boolean);
procedure SetBorderStyle(Value: TBorderStyle);
procedure SetCenter(Value: Boolean);
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure SetPicture(Value: TPicture);
procedure SetReadOnly(Value: Boolean);
procedure SetStretch(Value: Boolean);
procedure UpdateData(Sender: TObject);
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;
procedure WMCut(var Message: TMessage); message WM_CUT;
procedure WMCopy(var Message: TMessage); message WM_COPY;
procedure WMPaste(var Message: TMessage); message WM_PASTE;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
protected
procedure CreateParams(var Params: TCreateParams); override;
function GetPalette: HPALETTE; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure Paint; override;
function GetSmooth : Byte;
procedure SetSmooth(smooth : Byte);
function GetQuality : Byte;
procedure SetQuality(Quality : Byte);
function GetDither : Byte;
procedure SetDither(dith : Byte);
function GetRes : Byte;
procedure SetRes(res : Byte);
public
BFiletype : String;
Bwidth : Integer;
BHeight : Integer;
Bbitspixel : Integer;
Bplanes : Integer;
Bnumcolors : Integer;
BSize : Longint;
Bcompression : String;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure CopyToClipboard;
procedure CutToClipboard;
procedure LoadPicture;
procedure PasteFromClipboard;
procedure LoadFromFile(filename : TFilename);
procedure SaveToFile(filename : TFilename);
procedure SaveToFileAsBMP(filename : TFilename);
procedure SaveToFileAsJpeg(filename : TFilename);
function GetInfoAndType : String;
property Field: TField read GetField;
property Picture: TPicture read FPicture write SetPicture;
published
property JPegDither : Byte read GetDither write SetDither;
property JPegResolution : Byte read GetRes write SetRes;
property JPegSaveQuality : Byte read GetQuality write SetQuality;
property JPegSaveSmooth : Byte read GetSmooth write SetSmooth;
property UPdateBlobAsJpeg : Boolean read FUpdateAsJpeg write FUpdateAsJpeg;
property Align;
property AutoDisplay: Boolean read FAutoDisplay write SetAutoDisplay default True;
property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsSingle;
property Center: Boolean read FCenter write SetCenter default True;
property Color;
property Ctl3D;
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property ParentColor default False;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False;
property ShowHint;
property Stretch: Boolean read FStretch write SetStretch default False;
property TabOrder;
property TabStop default True;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
procedure Register;
var
TMultiImageCallBack : TCallBackFunction;
TDBMultiImageCallBack : TCallBackFunction;
{------------------------------------------------------------------------}
implementation
uses Consts, DBIErrs, DBITypes, Clipbrd, DBConsts, Dialogs;
{------------------------------------------------------------------------}
procedure Register;
begin
RegisterComponents('Add Ons',[TMultiImage]);
RegisterComponents('Add Ons',[TDBMultiImage]);
end;
{------------------------------------------------------------------------
TMultiImage.
------------------------------------------------------------------------}
constructor TMultiImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPicture := TPicture.Create;
FPicture.OnChange := PictureChanged;
FFilename:='';
Fdither:=4;
FResolution:=8;
FSaveQuality:=25;
FSaveSmooth:=0;
Picture.Graphic := nil;
Height := 105;
Width := 105;
end;
{------------------------------------------------------------------------}
destructor TMultiImage.Destroy;
begin
FPicture.Free;
inherited Destroy;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetPalette: HPALETTE;
begin
Result := 0;
if FPicture.Graphic is TBitmap then
Result := TBitmap(FPicture.Graphic).Palette;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.Paint;
var
Dest: TRect;
begin
if csDesigning in ComponentState then
with inherited Canvas do
begin
Pen.Style := psDash;
Brush.Style := bsClear;
Rectangle(0, 0, Width, Height);
end;
if Stretch then
Dest := ClientRect
else if Center then
Dest := Bounds((Width - Picture.Width) div 2, (Height - Picture.Height) div 2,
Picture.Width, Picture.Height)
else
Dest := Rect(0, 0, Picture.Width, Picture.Height);
with inherited Canvas do
StretchDraw(Dest, Picture.Graphic);
end;
{------------------------------------------------------------------------}
function TMultiImage.GetCanvas: TCanvas;
var
Bitmap: TBitmap;
begin
if Picture.Graphic = nil then
begin
Bitmap := TBitmap.Create;
try
Bitmap.Width := Width;
Bitmap.Height := Height;
Picture.Graphic := Bitmap;
finally
Bitmap.Free;
end;
end;
if Picture.Graphic is TBitmap then
Result := TBitmap(Picture.Graphic).Canvas
else
raise EInvalidOperation.Create(LoadStr(SImageCanvasNeedsBitmap));
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetAutoSize(Value: Boolean);
begin
FAutoSize := Value;
PictureChanged(Self);
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetCenter(Value: Boolean);
begin
if FCenter <> Value then
begin
FCenter := Value;
Invalidate;
end;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetPicture(Value: TPicture);
begin
FPicture.Assign(Value);
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetStretch(Value: Boolean);
begin
FStretch := Value;
Invalidate;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.PictureChanged(Sender: TObject);
begin
if AutoSize and (Picture.Width > 0) and (Picture.Height > 0) then
SetBounds(Left, Top, Picture.Width, Picture.Height);
if (Picture.Graphic is TBitmap) and (Picture.Width = Width) and
(Picture.Height = Height) then
ControlStyle := ControlStyle + [csOpaque] else
ControlStyle := ControlStyle - [csOpaque];
Invalidate;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetDither : Byte;
begin
GetDither:=Fdither
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetDither(dith : Byte);
begin
Fdither:=4;
case dith of
0..4 :Fdither:=dith;
end;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetRes : Byte;
begin
GetRes:=FResolution;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetRes(res : Byte);
begin
FResolution:=8;
case res of
4 :FResolution:=res;
8 :FResolution:=res;
24 :FResolution:=res;
end;
end;
{------------------------------------------------------------------------}
Procedure TMultiImage.WriteMultiName(Name : String);
begin
FFilename:=Name;
GetMultiBitmap;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetMultiBitmap : String;
var bitmap : TBitMap;
Pextension : string[4];
OnExcept : Boolean;
f : file of byte;
label BreakIt;
begin
OnExcept:=False;
if not FileExists(FFilename) then begin
Picture.Graphic := nil;
temps:='file not found';
GetMultiBitmap:=temps;
exit;
end;
if FResolution <> 4 then if FResolution <> 8 then if FResolution <> 24 then
FResolution:=8;
if (FDither < 0) or (FDither > 4) then FDither:=4;
Pextension:=UpperCase(ExtractFileExt(FFilename));
if (Pextension = '.WMF') or (Pextension = '.ICO') then begin
Picture.LoadFromFile(FFilename);
Temps:='Non JPeg, BMP, GIF or PCX Image';
GetMultiBitmap:=Temps;
GetInfoAndType(FFileName);
exit;
end;
if (UpperCase(FFilename) = temps) and (Picture.Bitmap <> nil) then
Goto BreakIt;
if Pextension = '.BMP' then begin
try
Bitmap := TBitmap.Create;
if not bmpfile(FFileName, Bitmap, TMultiImageCallBack) then
MessageDlg('Reading bmp file failed', mtInformation, [mbOk], 0);
except
Picture.Graphic := nil;
Bitmap.Free;
OnExcept:=True;
end;
if OnExcept then Goto BreakIt;
Picture.Graphic:=Bitmap;
Bitmap.Free;
GetInfoAndType(FFileName);
end;
if Pextension = '.GIF' then begin
try
Bitmap := TBitmap.Create;
if not Giffile(FFileName, Bitmap, TMultiImageCallBack) then
MessageDlg('Reading gif file failed', mtInformation, [mbOk], 0);
except
Picture.Graphic := nil;
Bitmap.Free;
OnExcept:=True;
end;
if OnExcept then Goto BreakIt;
Picture.Graphic:=Bitmap;
Bitmap.Free;
GetInfoAndType(FFileName);
end;
if Pextension = '.PCX' then begin
try
Bitmap := TBitmap.Create;
if not PCXfile(FFileName, Bitmap, TMultiImageCallBack) then
MessageDlg('Reading pcx file failed', mtInformation, [mbOk], 0);
except
Picture.Graphic := nil;
Bitmap.Free;
OnExcept:=True;
end;
if OnExcept then Goto BreakIt;
Picture.Graphic:=Bitmap;
Bitmap.Free;
GetInfoAndType(FFileName);
end;
if Pextension = '.JPG' then begin
try
Bitmap := TBitmap.Create;
if not jpgfile(FFilename, FResolution, Fdither, Bitmap, TMultiImageCallBack) then
MessageDlg('Reading jpg file failed', mtInformation, [mbOk], 0);
except
Picture.Graphic := nil;
Bitmap.Free;
OnExcept:=True;
end;
if OnExcept then Goto BreakIt;
Picture.Graphic:=Bitmap;
Bitmap.Free;
GetInfoAndType(FFileName);
end;
BreakIt:
Temps:=UpperCase(FFilename);
GetMultiBitmap:=Temps;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetSmooth : Byte;
begin
GetSmooth:=FSaveSmooth;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetSmooth(Smooth : Byte);
begin
if (Smooth > 100) or (Smooth < 0) then FSaveSmooth:=5 else
FSaveSmooth:=Smooth;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetQuality : Byte;
begin
GetQuality:=FSaveQuality;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetQuality(Quality : Byte);
begin
if (Quality > 100) OR (Quality < 1) then FSaveQuality:=25 else
FSaveQuality:=Quality;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetSaveFileName : TFilename;
begin
GetSaveFileName:=FSaveFileName;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SetSaveFileName(fn : TFilename);
begin
if fn <> '' then
FSaveFileName:=fn
else
FSaveFileName:='';
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SaveAsBMP(FN : TFileName);
begin
if fn <> '' then FSaveFileName:=fn;
try
if not putbmpfile(FSaveFileName, picture.Bitmap, TMultiImageCallBack) then
MessageDlg('Writing bmp file failed', mtInformation, [mbOk], 0);
except
end;
end;
{------------------------------------------------------------------------}
procedure TMultiImage.SaveAsJpg(FN : TFileName);
begin
if fn <> '' then FSaveFileName:=fn;
try
if not putjpgfile(FSaveFileName, FSaveQuality, FSaveSmooth, picture.Bitmap, TMultiImageCallBack) then
MessageDlg('Writing jpg file failed', mtInformation, [mbOk], 0);
except
end;
end;
{------------------------------------------------------------------------}
function TMultiImage.GetInfoAndType(filename : TFilename) : Boolean;
var
Pextension : string[4];
f : file of byte;
begin
Pextension:=UpperCase(ExtractFileExt(Filename));
if (Pextension = '.WMF') or (Pextension = '.ICO') then begin
if fileexists(Filename) then begin
Delete(Pextension,1,1);
BFiletype := Pextension;
Bwidth := Picture.width;
BHeight := Picture.Height;
Bbitspixel := 0;
Bplanes := 0;
Bnumcolors := 0;
Bcompression := Pextension;
AssignFile(f, FFileName);
Reset(f);
Bsize := FileSize(f);
CloseFile(f);
GetInfoAndType:=true;
exit;
end else begin
BFiletype := 'ERR';
Bwidth := -1;
BHeight := -1;
Bbitspixel := -1;
Bplanes := -1;
Bnumcolors := -1;
Bcompression := 'ERR';
Bsize := -1;
GetInfoAndType := false;
exit;
end;
end;
GetInfoAndType:=GetFileInfo(filename,
BFileType,
Bwidth,
BHeight,
Bbitspixel,
Bplanes,
Bnumcolors,
Bcompression);
AssignFile(f, FileName);
Reset(f);
Bsize := FileSize(f);
CloseFile(f);
end;
{------------------------------------------------------------------------
end TMultiImage
------------------------------------------------------------------------}
{TDBMultiImage}
constructor TDBMultiImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csFramed, csOpaque];
Width := 105;
Height := 105;
TabStop := True;
ParentColor := False;
FPicture := TPicture.Create;
FPicture.OnChange := PictureChanged;
FBorderStyle := bsSingle;
FAutoDisplay := True;
FCenter := True;
FUpdateAsJpeg := True;
Fdither:=4;
FResolution:=8;
FSaveQuality:=25;
FSaveSmooth:=0;
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnUpdateData := UpdateData;
end;
{------------------------------------------------------------------------}
destructor TDBMultiImage.Destroy;
begin
FPicture.Free;
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetDataSource(Value: TDataSource);
begin
FDataLink.DataSource := Value;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetReadOnly: Boolean;
begin
Result := FDataLink.ReadOnly;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetReadOnly(Value: Boolean);
begin
FDataLink.ReadOnly := Value;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetField: TField;
begin
Result := FDataLink.Field;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetPalette: HPALETTE;
begin
Result := 0;
if FPicture.Graphic is TBitmap then
Result := TBitmap(FPicture.Graphic).Palette;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetAutoDisplay(Value: Boolean);
begin
if FAutoDisplay <> Value then
begin
FAutoDisplay := Value;
if Value then LoadPicture;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetBorderStyle(Value: TBorderStyle);
begin
if FBorderStyle <> Value then
begin
FBorderStyle := Value;
RecreateWnd;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetCenter(Value: Boolean);
begin
if FCenter <> Value then
begin
FCenter := Value;
Invalidate;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetPicture(Value: TPicture);
begin
FPicture.Assign(Value);
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetStretch(Value: Boolean);
begin
if FStretch <> Value then
begin
FStretch := Value;
Invalidate;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.Paint;
var
W, H: Integer;
R: TRect;
S: string[63];
begin
with Canvas do
begin
Brush.Style := bsSolid;
Brush.Color := Color;
if FPictureLoaded then
begin
if Stretch then
if Picture.Graphic.Empty then
FillRect(ClientRect) else
StretchDraw(ClientRect, Picture.Graphic)
else
begin
SetRect(R, 0, 0, Picture.Width, Picture.Height);
if Center then OffsetRect(R, (ClientWidth - Picture.Width) div 2,
(ClientHeight - Picture.Height) div 2);
StretchDraw(R, Picture.Graphic);
ExcludeClipRect(Handle, R.Left, R.Top, R.Right, R.Bottom);
FillRect(ClientRect);
SelectClipRgn(Handle, 0);
end;
end else
begin
Font := Self.Font;
if FDataLink.Field <> nil then
S := FDataLink.Field.DisplayLabel else
S := Name;
S := '(' + S + ')';
W := TextWidth(S);
H := TextHeight(S);
R := ClientRect;
TextRect(R, (R.Right - W) div 2, (R.Bottom - H) div 2, S);
end;
if (GetParentForm(Self).ActiveControl = Self) and
not (csDesigning in ComponentState) then
begin
Brush.Color := clWindowFrame;
FrameRect(ClientRect);
end;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.PictureChanged(Sender: TObject);
begin
FDataLink.Modified;
FPictureLoaded := True;
Invalidate;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = DataSource) then DataSource := nil;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.LoadPicture;
var
Stream : TMemoryStream;
BitMap : TBitMap;
Cursor : hCursor;
temp : string;
begin
if not FPictureLoaded and (FDataLink.Field is TBlobField) then begin
if TBlobField(FDataLink.Field).IsNull then exit;
Temp:=GetInfoAndType;
SendMessage(Canvas.Handle, WM_Paint, 0, 0);
if Temp = 'GIF' then begin
Stream:=TMemoryStream.Create;
BitMap:=TBitMap.Create;
try
TBlobField(FDataLink.Field).SaveToStream(Stream);
Cursor := SetCursor(LoadCursor(0,idc_Wait));
if not gifblob(Stream.Memory,Stream.Size, Bitmap, TDBMultiImageCallBack) then begin
MessageDlg('Invallid or empty GIF blobfield', mtInformation, [mbOk], 0);
Picture.Assign(Nil);
end else
Picture.Assign(BitMap);
finally
SetCursor(Cursor);
BitMap.free;
Stream.Free;
end;
end else
if Temp = 'PCX' then begin
Stream:=TMemoryStream.Create;
BitMap:=TBitMap.Create;
try
TBlobField(FDataLink.Field).SaveToStream(Stream);
Cursor := SetCursor(LoadCursor(0,idc_Wait));
if not pcxblob(Stream.Memory,Stream.Size, Bitmap, TDBMultiImageCallBack) then begin
MessageDlg('Invallid or empty PCX blobfield', mtInformation, [mbOk], 0);
Picture.Assign(Nil);
end else
Picture.Assign(BitMap);
finally
SetCursor(Cursor);
BitMap.free;
Stream.Free;
end;
end else
if Temp = 'BMP' then begin
Stream:=TMemoryStream.Create;
BitMap:=TBitMap.Create;
try
TBlobField(FDataLink.Field).SaveToStream(Stream);
Cursor := SetCursor(LoadCursor(0,idc_Wait));
if not bmpblob(Stream.Memory,Stream.Size, Bitmap, TDBMultiImageCallBack) then begin
MessageDlg('Invallid or empty BMP blobfield', mtInformation, [mbOk], 0);
Picture.Assign(Nil);
end else
Picture.Assign(BitMap);
finally
SetCursor(Cursor);
BitMap.free;
Stream.Free;
end;
end else
if Temp = 'JPG' then begin
Stream:=TMemoryStream.Create;
BitMap:=TBitMap.Create;
if FResolution <> 4 then
if FResolution <> 8 then
if FResolution <> 24 then FResolution:=8;
if (FDither < 0) or (FDither > 4) then FDither:=4;
try
TBlobField(FDataLink.Field).SaveToStream(Stream);
Cursor := SetCursor(LoadCursor(0,idc_Wait));
if not jpgblob(Stream.Memory,Stream.Size, FResolution, Fdither, Bitmap, TDBMultiImageCallBack) then begin
MessageDlg('Invallid or empty Jpeg Blobfield', mtInformation, [mbOk], 0);
Picture.Assign(Nil);
end else
Picture.Assign(BitMap);
finally
SetCursor(Cursor);
BitMap.free;
Stream.Free;
end;
end;
GetInfoAndType;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.DataChange(Sender: TObject);
begin
Picture.Graphic := nil;
FPictureLoaded := False;
if FAutoDisplay then LoadPicture;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.UpdateData(Sender: TObject);
var
Stream : TMemoryStream;
Cursor : hCursor;
Usize : longInt;
x,y : longInt;
p : Pointer;
begin
if FDataLink.Field is TBlobField then begin
if Picture.Graphic is TBitmap then begin
x:=Picture.BitMap.Width;
y:=Picture.BitMap.Height;
y:=y+(y div 5);
x:=x+(x div 5);
Usize:=(y * x);
if Usize < 90000 then Usize:=Usize*2;
{Since we can't know how much memory we need to allocate
to write the picture to the stream we need to guess it. This
is done using the width and height of the bitmap. After the call
to the dll using PUTJPGBLOB or PUTBMPBLOB Usize contains the
correct size of the Jpeg stored in P^. You can increase or decrease
the guessed memory by altering the Div by. For instance
y:=y+(y div 3);
x:=x+(x div 3);
will allocate more memory then
y:=y+(y div 6);
x:=x+(x div 6);
We played it on the save side. Use this "guess work" very carefully}
P := GlobalAllocPtr(HeapAllocFlags, Usize);
if P = Nil then begin
MessageDlg('Error allocation blob memory', mtInformation, [mbOk], 0);
exit;
end;
if FUpdateAsJpeg then begin
if not putjpgblob(P, USize, FSaveQuality, FSaveSmooth, Picture.Bitmap, TDBMultiImageCallBack) then
MessageDlg('Jpeg BLOB Write Error', mtInformation, [mbOk], 0);
end else begin
if not putbmpblob(P, USize, Picture.Bitmap, TDBMultiImageCallBack) then
MessageDlg('BMP BLOB Write Error', mtInformation, [mbOk], 0);
end;
Stream:=TMemoryStream.Create;
Stream.Write(P^,USize);
GlobalFreePtr(P);
try
TBlobField(FDataLink.Field).LoadFromStream(Stream);
finally
Stream.Free;
end;
end else
TBlobField(FDataLink.Field).Clear;
end;
GetInfoAndType;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.CopyToClipboard;
begin
if Picture.Graphic <> nil then Clipboard.Assign(Picture);
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.CutToClipboard;
begin
if Picture.Graphic <> nil then
begin
CopyToClipboard;
if FDataLink.Edit then
Picture.Graphic := nil;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.PasteFromClipboard;
begin
if Clipboard.HasFormat(CF_PICTURE) and FDataLink.Edit then
Picture.Assign(Clipboard);
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
if FBorderStyle = bsSingle then
Params.Style := Params.Style or WS_BORDER;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
case Key of
VK_INSERT:
if ssShift in Shift then PasteFromClipBoard else
if ssCtrl in Shift then CopyToClipBoard;
VK_DELETE:
if ssShift in Shift then CutToClipBoard;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
case Key of
^X: CutToClipBoard;
^C: CopyToClipBoard;
^V: PasteFromClipBoard;
#13: LoadPicture;
#27: FDataLink.Reset;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.CMEnter(var Message: TCMEnter);
begin
Invalidate; { Draw the focus marker }
inherited;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.CMExit(var Message: TCMExit);
begin
Invalidate; { Erase the focus marker }
inherited;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.CMTextChanged(var Message: TMessage);
begin
inherited;
if not FPictureLoaded then Invalidate;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.WMLButtonDown(var Message: TWMLButtonDown);
begin
if TabStop and CanFocus then SetFocus;
inherited;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.WMLButtonDblClk(var Message: TWMLButtonDblClk);
begin
LoadPicture;
inherited;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.WMCut(var Message: TMessage);
begin
CutToClipboard;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.WMCopy(var Message: TMessage);
begin
CopyToClipboard;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.WMPaste(var Message: TMessage);
begin
PasteFromClipboard;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.LoadFromFile(filename : TFilename);
var
Cursor : hCursor;
begin
if not FileExists(filename) then begin
MessageDlg('File not found', mtInformation, [mbOk], 0);
exit;
end;
if UpperCase(ExtractFileExt(filename)) <> '.JPG' then
if UpperCase(ExtractFileExt(filename)) <> '.GIF' then
if UpperCase(ExtractFileExt(filename)) <> '.PCX' then
if UpperCase(ExtractFileExt(filename)) <> '.BMP' then
begin
MessageDlg('Not a Jpeg, Gif, Pcx or Bmp File', mtInformation, [mbOk], 0);
exit;
end;
Cursor := SetCursor(LoadCursor(0,idc_Wait));
if FDataLink.Field is TBlobField then
TBlobField(FDataLink.Field).LoadFromFile(filename)
else begin
SetCursor(Cursor);
MessageDlg('Field is not a blobfield', mtInformation, [mbOk], 0);
exit;
end;
GetInfoAndType;
SetCursor(Cursor);
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SaveToFile(filename : TFilename);
var
Cursor : hCursor;
begin
if FDataLink.Field is TBlobField then begin
if TBlobField(FDataLink.Field).IsNull then begin
MessageDlg('Can''t save, blobfield is empty', mtInformation, [mbOk], 0);
exit;
end;
Cursor := SetCursor(LoadCursor(0,idc_Wait));
TBlobField(FDataLink.Field).SaveToFile(filename);
GetInfoAndType;
SetCursor(Cursor)
end else begin
MessageDlg('Field is not a blobfield', mtInformation, [mbOk], 0);
exit;
end;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SaveToFileAsBMP(filename : TFilename);
var
Cursor : hCursor;
begin
if FDataLink.Field is TBlobField then begin
if TBlobField(FDataLink.Field).IsNull then begin
MessageDlg('Can''t save, blobfield bitmap is empty', mtInformation, [mbOk], 0);
exit;
end;
if picture.bitmap.empty then begin
MessageDlg('Can''t save, image is not displayed, Set Autodisplay or double click display to view image first.',
mtInformation, [mbOk], 0);
exit;
end;
Cursor := SetCursor(LoadCursor(0,idc_Wait));
if not putbmpfile(FileName, picture.Bitmap, TDBMultiImageCallBack) then begin
SetCursor(Cursor);
MessageDlg('Writing bmp file failed', mtInformation, [mbOk], 0);
exit;
end;
GetInfoAndType
end else begin
SetCursor(Cursor);
MessageDlg('Field is not a blobfield', mtInformation, [mbOk], 0);
exit;
end;
SetCursor(Cursor);
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SaveToFileAsJpeg(filename : TFilename);
var
Cursor : hCursor;
begin
if FDataLink.Field is TBlobField then begin
if TBlobField(FDataLink.Field).IsNull then begin
MessageDlg('Can''t save, blobfield bitmap is empty', mtInformation, [mbOk], 0);
exit;
end;
if picture.bitmap = nil then begin
MessageDlg('Can''t save, image is not displayed', mtInformation, [mbOk], 0);
exit;
end;
Cursor := SetCursor(LoadCursor(0,idc_Wait));
if not putjpgfile(FileName, FSaveQuality, FSaveSmooth, picture.Bitmap, TDBMultiImageCallBack) then begin
SetCursor(Cursor);
MessageDlg('Writing jpg file failed', mtInformation, [mbOk], 0);
exit;
end;
GetInfoAndType
end else begin
SetCursor(Cursor);
MessageDlg('Field is not a blobfield', mtInformation, [mbOk], 0);
exit;
end;
SetCursor(Cursor);
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetInfoAndType : String;
var
Stream : TMemoryStream;
begin
if (FDataLink.Field is TBlobField) then
if TBlobField(FDataLink.Field).IsNull then exit;
BFileType := 'Empty';
Bwidth:=-1;
BHeight:=-1;
Bbitspixel:=-1;
Bplanes:=-1;
Bnumcolors:=-1;
Bcompression:='-1';
BSize:=-1;
GetInfoAndType :='-1';
Stream:=TMemoryStream.Create;
TBlobField(FDataLink.Field).SaveToStream(Stream);
if not GetBlobInfo(Stream.Memory,
Stream.Size,
BFileType,
Bwidth,
BHeight,
Bbitspixel,
Bplanes,
Bnumcolors,
Bcompression) then
MessageDlg('blobfield getinfo failed', mtInformation, [mbOk], 0) else
begin
BSize:=Stream.Size;
if UpperCase(BFileType) = 'GIF' then GetInfoAndType:='GIF' else
if UpperCase(BFileType) = 'PCX' then GetInfoAndType:='PCX' else
if UpperCase(BFileType) = 'JPEG' then GetInfoAndType:='JPG' else
if UpperCase(BFileType) = 'BMP' then GetInfoAndType:='BMP';
end;
if Stream.Memory <> nil then Stream.Free;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetSmooth : Byte;
begin
GetSmooth:=FSaveSmooth;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetSmooth(Smooth : Byte);
begin
if (Smooth > 100) or (Smooth < 0) then FSaveSmooth:=5 else
FSaveSmooth:=Smooth;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetQuality : Byte;
begin
GetQuality:=FSaveQuality;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetQuality(Quality : Byte);
begin
if (Quality > 100) OR (Quality < 1) then FSaveQuality:=25 else
FSaveQuality:=Quality;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetDither : Byte;
begin
GetDither:=Fdither
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetDither(dith : Byte);
begin
Fdither:=4;
case dith of
0..4 :Fdither:=dith;
end;
end;
{------------------------------------------------------------------------}
function TDBMultiImage.GetRes : Byte;
begin
GetRes:=FResolution;
end;
{------------------------------------------------------------------------}
procedure TDBMultiImage.SetRes(res : Byte);
begin
FResolution:=8;
case res of
4 :FResolution:=res;
8 :FResolution:=res;
24:FResolution:=res;
end;
end;
{------------------------------------------------------------------------}
begin
TMultiImageCallBack:=nil;
TDBMultiImageCallBack:=nil;
end.